package com.doit.day02;

import com.doit.day01.JedisUtil;
import org.apache.commons.lang3.RandomUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Tuple;

import java.util.ArrayList;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

/**
 * 我们腾讯公司出了一款游戏叫lol
 * 每次比赛用户会随机选择10名英雄参比赛
 * 实时的统计每个英雄出场的次数，取前3名  每隔5s返回一次
 *
 * 我们要针对这前3名英雄设定皮肤，卖
 *
 * 1.模拟英雄出场  ==》 arraylist   里面放20个英雄
 * 2.收集起来排序，排完之后取前3名
 * 3.每隔5s钟返回结果
 */
public class _01_需求_分组topn {
    public static void main(String[] args) throws InterruptedException {
//        String [] arr = {"a","b","c","d","e","f","g","h","i","j","k"};
        ArrayList<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        list.add("e");
        list.add("f");
        list.add("g");
        list.add("h");
        list.add("i");
        list.add("j");
        list.add("k");
        list.add("l");
        list.add("m");
        list.add("n");
        list.add("o");
        Jedis jedis = JedisUtil.getJedis();

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true){
                    //随机获取英雄
                    String hero = list.get(RandomUtils.nextInt(0, list.size()));
                    //将英雄都收集起来，放到redis里面==》 value用什么数据类型 sorted set
                    jedis.zincrby("hero",1,hero);
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                Set<Tuple> hero = jedis.zrevrangeWithScores("hero", 0, 2);
                for (Tuple tuple : hero) {
                    String element = tuple.getElement();
                    double score = tuple.getScore();
                    System.out.println(element+"=>"+score);
                }
                System.out.println("=========帅气的分隔符==========");
            }
        },2000,5000);







    }
}
